//
//  BBTKBaseSplitViewController.m
//  AFNetworking
//
//  Created by aron on 2020/4/20.
//

#import "BBTKBaseSplitViewController.h"
#import "BBTKLeftMenuViewController.h"
#import "BBTKSplitViewController.h"
#import <BBSDK/NSObject+BBKVCWrapper.h>
#import <Masonry/Masonry.h>
//#import <DoraemonKit/DoraemonAppInfoUtil.h>

@interface BBTKBaseSplitViewController ()

@property (nonatomic, copy) NSArray<BBTKSplitItem *> *datas;

@property (nonatomic, strong) BBTKSplitViewController *splitVC;
@property (nonatomic, strong) BBTKLeftMenuViewController *leftMenuVC;

@end

@implementation BBTKBaseSplitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    BBTKLeftMenuViewController *leftMenuVC = [[BBTKLeftMenuViewController alloc] init];
    __weak __typeof(self)weakSelf = self;
    leftMenuVC.itemClickBlock = ^(NSInteger index) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf udpateWithIndex:index];
    };
    self.leftMenuVC = leftMenuVC;
    
    UIViewController *colorVC = [[UIViewController alloc] init];
    UINavigationController *colorNavi = [[UINavigationController alloc] initWithRootViewController:colorVC];
    
    BBTKSplitViewController *splitVC = [[BBTKSplitViewController alloc] initWithLeftViewController:leftMenuVC rightViewController:colorNavi];
    CGFloat leftSplitWidth = 160;
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait
        && (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPhone)) {
        // iPhone，竖屏布局
        leftSplitWidth = 100;
    } else {
        // 横屏布局
        leftSplitWidth = 160;
    }
    splitVC.leftSplitWidth = leftSplitWidth;
    self.splitVC = splitVC;

    [self addChildViewController:splitVC];
    [self.view addSubview:splitVC.view];
    
    [splitVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.top.equalTo(self.view);
    }];
}

/// 设置数据
/// @param datas 数据
- (void)setupDatas:(NSArray<BBTKSplitItem *> *)datas {
    _datas = datas;
    [self.leftMenuVC setupDatas:_datas];
    
    [self udpateWithIndex:0];
}

// MARK: - Private

- (void)udpateWithIndex:(NSInteger)index {
    // 设置Menu选中效果
    [self.leftMenuVC setSelectedIndex:index];
    
    // 创建子页面
    BBTKSplitItem *item = self.datas[index];
    UIViewController *detailVC = [item.vcClass new];
    if (![detailVC isKindOfClass:UIViewController.class]) {
        detailVC = [UIViewController new];
    }
    
    // 设置子页面数据
    NSDictionary *info = @{@"actionCellModels": item.subItems};
    if ([info isKindOfClass:NSDictionary.class]) {
        [detailVC bb_setPropertyWithDictionary:info];
    }
    
    // 设置子页面
    [self.splitVC setRightController:detailVC];
}

@end
